/********************************************************************************
 * Project Name		[JavaSE_Fundamentals]
 * File Name     	[TestEvent.java]
 * Creation Date 	[2015-01-01]
 * 
 * Copyright© ge.y.yang@gmail.com All Rights Reserved
 * 
 * Work hard, play harder, think big and keep fit
 ********************************************************************************/
package swing.awt;

import java.awt.Color;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

/**
 * @author 不落的太阳(Sean Yang aka ShortPeace)
 * @version 1.0
 * @since jdk 1.8
 * 
 */
public class TestEvent {

	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			@Override
			public void run() {
				ButtonFrame1 frame1 = new ButtonFrame1();
				frame1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
				frame1.setVisible(true);

				ButtonFrame2 frame2 = new ButtonFrame2();
				frame2.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
				frame2.setVisible(true);
			}
		});
	}
}

/**
 * 
 * @author 不落的太阳(Sean Yang aka ShortPeace)
 * @version 1.0
 * @since jdk 1.8
 *
 */
class ButtonFrame1 extends JFrame {

	private static final long serialVersionUID = -4276293176087454708L;

	public ButtonFrame1() {
		this.setTitle("Button Click Event");
		this.setSize(300, 200);
		this.setLocationByPlatform(true);
		this.setResizable(false);
		this.getContentPane().add(new ButtonPanel1());
	}
}

/**
 * 
 * @author 不落的太阳(Sean Yang aka ShortPeace)
 * @version 1.0
 * @since jdk 1.8
 *
 */
class ButtonPanel1 extends JPanel {

	private static final long serialVersionUID = 6584483859136055453L;

	private JButton redButton = null;
	private JButton blueButton = null;
	private JButton yellowButton = null;

	public ButtonPanel1() {
		redButton = new JButton("RED");
		redButton.addActionListener(new ColorActionListener(Color.RED));
		this.add(redButton);

		blueButton = new JButton("BLUE");
		blueButton.addActionListener(new ColorActionListener(Color.BLUE));
		this.add(blueButton);

		yellowButton = new JButton("YELLOW");
		yellowButton.addActionListener(new ColorActionListener(Color.YELLOW));
		this.add(yellowButton);
	}

	private class ColorActionListener implements ActionListener {

		private Color backgroundColor = null;

		public ColorActionListener(Color color) {
			backgroundColor = color;
		}

		@Override
		public void actionPerformed(ActionEvent e) {
			ButtonPanel1.this.setBackground(backgroundColor);
		}
	}
}

/**
 * 
 * @author 不落的太阳(Sean Yang aka ShortPeace)
 * @version 1.0
 * @since jdk 1.8
 *
 */
class ButtonFrame2 extends JFrame {

	private static final long serialVersionUID = 5262965834731194665L;

	private JPanel actionPanel = null;

	public ButtonFrame2() {
		this.setTitle("Button Click Event");
		this.setSize(300, 200);
		this.setLocationByPlatform(true);
		this.setResizable(false);

		actionPanel = new JPanel();
		this.generateButton("RED", Color.RED);
		this.generateButton("BLUE", Color.BLUE);
		this.generateButton("YELLOW", Color.YELLOW);

		this.getContentPane().add(actionPanel);
	}

	/**
	 * 生成带有事件监听的按钮
	 * 
	 * @param buttonName
	 *            按钮名称
	 * @param backgroundColor
	 *            背景色
	 */
	private void generateButton(String buttonName, final Color backgroundColor) {
		JButton button = new JButton(buttonName);
		this.actionPanel.add(button);
		button.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				actionPanel.setBackground(backgroundColor);
			}
		});
	}
}
